package com.baomidou.springmvc.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springmvc.model.system.UserView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.springmvc.model.enums.TypeEnum;
import com.baomidou.springmvc.model.system.User;
import com.baomidou.springmvc.service.system.IUserService;

import java.util.List;

/**
 * Author: D.Yang
 * Email: koyangslash@gmail.com
 * Date: 16/10/9
 * Time: 上午11:58
 * Describe: 用户控制器
 * <p>
 * 代码生成器，参考源码测试用例：
 * <p>
 * /mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java
 */
@Controller
public class UserController extends BaseController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    // 首页
    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", userService.selectList(null));
        return modelAndView;
    }

    // 测试内置单表分页查询
    @RequestMapping(value = "/buildinPage")
    public ModelAndView buildinPage(ModelAndView modelAndView,
                                    @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
                                    @RequestParam(name = "state", required = false, defaultValue = "1") String state) {
        Page<User> pageParam = new Page<>();
        pageParam.setOffset(Math.max(0, (page - 1) * rows));
        pageParam.setLimit(rows);
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        userService.buildinSelectUserPage(pageParam, state);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<User> list = pageParam.getRecords();
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", list);
        return modelAndView;
    }

    // 测试分页查询
    @RequestMapping(value = "/", params = {"page", "rows"})
    public ModelAndView indexPage(ModelAndView modelAndView, int page, int rows,
                                  @RequestParam(name = "state", required = false, defaultValue = "1") String state) {
        Page<User> pageParam = new Page<>();
        pageParam.setOffset(Math.max(0, (page - 1) * rows));
        pageParam.setLimit(rows);
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        userService.selectUserPage(pageParam, state);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<User> list = pageParam.getRecords();
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", list);
        return modelAndView;
    }

    // 测试分页查询并返回自定义JavaBean
    @RequestMapping(value = "/myPagination")
    public ModelAndView myPagination(ModelAndView modelAndView,
                                     @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                     @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
                                     @RequestParam(name = "state", required = false, defaultValue = "1") String state) {
        Page<UserView> pageParam = new Page<>();
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        userService.selectUserView(pageParam, state);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        List<UserView> list = pageParam.getRecords();
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", list);
        return modelAndView;
    }

    // FastJsonHttpMessageConverter4 测试及 Enum 格式化测试
    @RequestMapping(value = "/myPaginationData")
    @ResponseBody
    public Object myPaginationData(
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,
            @RequestParam(name = "state", required = false, defaultValue = "1") String state) {
        Page<UserView> pageParam = new Page<>();
        pageParam.setCurrent(page);
        pageParam.setSize(rows);
        userService.selectUserView(pageParam, state);
        System.out.println("offset: " + pageParam.getOffset());
        System.out.println("limit: " + pageParam.getLimit());
        System.out.println("total: " + pageParam.getTotal());

        return pageParam;
    }

    // 测试修改页面
    @RequestMapping("/preSave")
    public ModelAndView preSave(ModelAndView modelAndView,
                                @RequestParam(value = "id", required = false) Long id) {
        modelAndView.setViewName("save");
        if (id != null) {
            modelAndView.addObject("user", userService.selectById(id));
        }
        return modelAndView;
    }

    // 测试新增和修改操作
    @ResponseBody
    @RequestMapping("save")
    public Object save(User user) {
        user.setType(TypeEnum.DISABLED);
        if (user.getId() == null) {
            return userService.insert(user) ? renderSuccess("添加成功") : renderError("添加失败");
        } else {
            return userService.updateById(user) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }

    // 测试删除操作
    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "id", required = false) Long id) {
        return userService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

    // 测试CRUD
    @RequestMapping("/crud")
    public ModelAndView crud(ModelAndView modelAndView) {
        userService.crud();
        return index(modelAndView);
    }

    // 测试CRUD异常回滚
    @RequestMapping("/crudRollback")
    public ModelAndView crudRollback(ModelAndView modelAndView) {
        try {
            userService.crudRollback();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return index(modelAndView);
    }
}
